package com.feiyuan.modules.sys.service.impl;

import com.feiyuan.common.exception.FeiyuanException;
import com.feiyuan.modules.sys.service.RoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feiyuan.common.utils.PageUtils;
import com.feiyuan.common.utils.Query;

import com.feiyuan.modules.sys.mapper.RoleDao;
import com.feiyuan.modules.sys.entity.RoleEntity;
import com.feiyuan.modules.sys.service.RoleService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String roleCode = (String) params.get("roleCode");
        String roleName = (String) params.get("roleName");
        IPage<RoleEntity> page = this.page(
                new Query<RoleEntity>().getPage(params),
                new QueryWrapper<RoleEntity>().like(StringUtils.isNotEmpty(roleCode), "role_code", roleCode)
                        .like(StringUtils.isNotEmpty(roleName), "role_name", roleName)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(RoleEntity role) {
        // 角色代码不能重复
        if (StringUtils.isNotEmpty(role.getRoleCode())) {
            RoleEntity roleCode = baseMapper.selectOne(new QueryWrapper<RoleEntity>().eq("role_code", role.getRoleCode()));
            if (roleCode != null) {
                throw new FeiyuanException("角色代码不能重复!", 500);
            }
        } else {
            throw new FeiyuanException("角色代码不能为空!", 500);
        }
        baseMapper.insert(role);

        // 保存角色与菜单
        if (!CollectionUtils.isEmpty(role.getMenuIds())) {
            roleMenuService.saveRoleMenu(role.getId(), role.getMenuIds());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByIdRole(RoleEntity role) {
        if (this.updateById(role)) {
            if (!CollectionUtils.isEmpty(role.getMenuIds())) {
                roleMenuService.updateRoleMenu(role.getId(), role.getMenuIds());
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeByIdsRole(List<Long> roleIds) {
        if (this.removeByIds(roleIds)) {
            roleMenuService.deleteRoleMenu(roleIds);
        }
    }

    @Override
    public RoleEntity getByIdInfo(Long id) {
        RoleEntity roleEntity = baseMapper.selectById(id);
        // 查询权限
        List<Long> menuIds = roleMenuService.selectByRoleId(id);
        roleEntity.setMenuIds(menuIds);
        return roleEntity;
    }

}